# from application.db import get_database_connection
# from application.db.mysql_db.article_ch.CnArticleAbstractInfo import CnArticleAbstractInfo
# from application.db.mysql_db.article_ch.CnArticleAuthorList import CnArticleAuthorList
# from application.db.mysql_db.article_ch.CnArticleAuthorPlatformIdRelation import CnArticleAuthorPlatformIdRelation
# from application.db.mysql_db.article_ch.CnArticleAuthorRelation import CnArticleAuthorRelation
# from application.db.mysql_db.article_ch.CnArticleAuthorUnitList import CnArticleAuthorUnitList
# from application.db.mysql_db.article_ch.CnArticleGrantRelation import CnArticleGrantRelation
# from application.db.mysql_db.article_ch.CnArticleJournalPlatformIdRelation import CnArticleJournalPlatformIdRelation
# from application.db.mysql_db.article_ch.CnArticleKeywordsList import CnArticleKeywordsList
# from application.db.mysql_db.article_ch.CnArticleKeywordsRelation import CnArticleKeywordsRelation
# from application.db.mysql_db.article_ch.CnArticleList import CnArticleList
# from application.db.mysql_db.article_ch.CnArticleReferenceList import CnArticleReferenceList
# from application.db.mysql_db.article_ch.CnArticleTypeDict import CnArticleTypeDict
# from application.db.mysql_db.article_ch.CnArticleTypeRelation import CnArticleTypeRelation
# from application.db.mysql_db.article_ch.CnArticleUnitPlatformIdRelation import CnArticleUnitPlatformIdRelation
#
# database = get_database_connection('default2')  # 使用默认数据库
# database.create_tables(
#     [CnArticleAuthorList, CnArticleAuthorRelation, CnArticleAuthorUnitList, CnArticleJournalPlatformIdRelation,
#      CnArticleKeywordsList, CnArticleList, CnArticleTypeDict, CnArticleUnitPlatformIdRelation,
#      CnArticleAuthorPlatformIdRelation, CnArticleTypeRelation, CnArticleReferenceList, CnArticleAbstractInfo,
#      CnArticleKeywordsRelation,CnArticleGrantRelation])  # 如果表已存在，不会重复创建
import json

from application.db.mysql_db.article_ch.CnArticleAuthorList import CnArticleAuthorList
from application.db.mysql_db.article_ch.CnArticleAuthorPlatformIdRelation import CnArticleAuthorPlatformIdRelation
from application.db.mysql_db.article_ch.CnArticleAuthorRelation import CnArticleAuthorRelation
from application.db.mysql_db.article_ch.CnArticleAuthorUnitList import CnArticleAuthorUnitList
from application.utils.utils import generate_random_id

import pandas as pd


def transformation_author(item):
    author_id = item.get('id')
    author = item.get('author')
    unit_info = author.get('unit_info')
    unit_info_names = [i.get('unit_name') for i in unit_info]
    yixue = "yixue"
    unit_info_name_dict = {}
    cn_a_u = CnArticleAuthorUnitList.select(CnArticleAuthorUnitList.unit_id, CnArticleAuthorUnitList.unit_name).where(
        CnArticleAuthorUnitList.unit_name.in_(unit_info_names)).dicts()
    for i in cn_a_u:
        unit_info_name_dict[i.get('unit_name')] = i.get('unit_id')

    for unit in unit_info:
        if unit.get('unit_name') in unit_info_name_dict:
            unit['unit_id'] = unit_info_name_dict[unit.get('unit_name')]
        else:
            unit['unit_id'] = generate_random_id('JG')
            unit_info_name_dict[unit.get('unit_name')] = unit['unit_id']
            CnArticleAuthorUnitList.insert({'unit_id': unit['unit_id'], 'unit_name': unit['unit_name'], "platform_id": {
                yixue: unit['platform_id']}}).execute()

    platform_id = author.get('platform_info').get('platform_id')

    exists = CnArticleAuthorPlatformIdRelation.select(
        CnArticleAuthorPlatformIdRelation.author_id
    ).where(
        CnArticleAuthorPlatformIdRelation.platform_id == platform_id
    ).first()
    if exists:
        author['author_id'] = exists.author_id
    if not author.get('author_id'):
        cn_a_l = CnArticleAuthorList.select(CnArticleAuthorList.author_id).where(
            CnArticleAuthorList.author_name == author.get('author_name')
        ).dicts()
        author_ids = [i.get('author_id') for i in cn_a_l]
        cn_a_r = CnArticleAuthorRelation.select(CnArticleAuthorRelation.author_id,
                                                CnArticleAuthorRelation.unit_id).where(
            CnArticleAuthorRelation.author_id.in_(author_ids)
        ).dicts()
        for i in cn_a_r:
            if i.get('unit_id') in unit_info_name_dict.values():
                author['author_id'] = i.get('author_id')
                break
    if not author.get('author_id'):
        author['author_id'] = generate_random_id('cn')
        CnArticleAuthorList.insert({
            'author_id': author.get('author_id'),
            'author_name': author.get('author_name'),
            'author_name_eng': author.get('author_name_eng'),
            'last_name': author.get('last_name'),
            'fore_name': author.get('fore_name'),
            'author_type': author.get('author_type'),
            'author_desc': ''
        }).execute()
        CnArticleAuthorPlatformIdRelation.insert({
            'author_id': author.get('author_id'),
            'platform_id': platform_id,
            'platform_code': author.get('platform_info').get('platform_code')
        }).execute()
    author['article_id'] = author_id
    return author

    # print(json.dumps(item, indent=4))


def get_author(item):
    result_list=[]
    unit_info = item.get('unit_info')
    for index, i in enumerate(unit_info):
            result_list.append({
                'article_id': item.get('id'),
                'author_id': item.get('author_id'),
                'unit_id': i.get('unit_id'),
                # "author_order": index_a,
                'unit_order': index,
                'corresponding_author': item.get('corresponding_author'),
                "author_email": item.get('author_email'),
                "is_display": item.get('is_display'),
            })
    return result_list



df = pd.read_json(r'D:\company_project\prefect_object\author.json')

df['author'] = df.apply(transformation_author, axis=1)
# df['author'] = df['author'].apply(get_author)
# for i in df['author'].values:
#     print(i)
# # print(json.dumps(df.to_dict(orient='records'), indent=4, ensure_ascii=False))
